package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.jakewharton.rxbinding2.widget.RxTextView
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_addfriendsgroup.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.zfw.SearchForFirendActivity
import lium.lbs.lbsim.zfw.SearchForGourpActivity
import java.util.concurrent.TimeUnit

class AddFriendsGroupActivity : BaseActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_addfriendsgroup
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("添加好友")
        addOnClickListeners(R.id.search_person, R.id.search_addgroup, R.id.open_phone_contacts)

        addDisposable(
            RxTextView.textChanges(et_add_searchtext)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe {
                    if (TextUtils.isEmpty(it)) {
                        open_phone_contacts.visibility = View.VISIBLE
                        search_layout.visibility = View.GONE
                    } else {
                        open_phone_contacts.visibility = View.GONE
                        search_layout.visibility = View.VISIBLE
                    }

                    tv_search_user.text = "找人：$it"
                    tv_search_group.text = "找群：$it"

                })
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.search_person -> {
                search(1)
            }
            R.id.search_addgroup -> {
                search(2)
            }
            R.id.open_phone_contacts -> {
                search(3)
            }


        }
    }

    //类型。1：找人；2：找群
    private fun search(type: Int) {

        when (type) {
            1 -> {
                val intent = Intent(this, SearchForFirendActivity::class.java)
                intent.putExtra("keywords", et_add_searchtext.text.toString())
                intent.putExtra("type", type)
                startActivity(intent)
            }
            2 -> {
                val intent = Intent(this, SearchForGourpActivity::class.java)
                intent.putExtra("keywords", et_add_searchtext.text.toString())
                intent.putExtra("type", type)
                startActivity(intent)
            }
            3 -> {
                val intent = Intent(this, GetTelNumberActivity::class.java)
                startActivity(intent)
            }
        }
    }

    override fun getNetWorkData() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 100) {
            finish()
        }
    }


}

